package uires
{
	
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.URLRequest;
	import flash.net.URLStream;
	import flash.utils.ByteArray;
	
	/***
	 *
	 * des:
	 * by: guwanyuan(顾万圆)
	 * 2016-3-12 下午8:15:01
	 ***/
	public class LoginStreamLoader extends URLStream 
	{
		public function LoginStreamLoader(complete:Function = null, progress:Function = null, error:Function = null)
		{
			_complete = complete;
			_progress = progress;
			_error = error;
		}

		public function get totalBytes():Number {
			return _totalBytes;
		}

		public function get finishLoadDate():Date {
			return _finishLoadDate;
		}

		public function get startLoadDate():Date {
			return _startLoadDate;
		}

		public var isMainSwf:Boolean;
		override public function load(request:URLRequest):void
		{
//			var url:String = request.url;
//			var bytes:ByteArray = ResourceLocalStore.getResFormLocal(url) as ByteArray;
//			if(bytes){
//				_data = bytes;
//				_complete(this);
//				return ;
//			}
			_request = request;
			this.addEventListener(Event.COMPLETE, onComplete);
			this.addEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			this.addEventListener(IOErrorEvent.IO_ERROR, onIoError);
			this.addEventListener(ProgressEvent.PROGRESS, onProgress);
			super.load(request);
			_startLoadDate = new Date();
		}

		protected function onProgress(event:ProgressEvent):void {
			_totalBytes = event.bytesTotal;
			if(_progress != null) {
				_progress(this, event);
			}
		}

		public function get useTime():int {
			if(_finishLoadDate && _startLoadDate) {
				return _finishLoadDate.getTime() - _startLoadDate.getTime();
			}
			return -1;
		}

		private function onComplete(e:Event):void 
		{
			if(isMainSwf)
			{
				var data:ByteArray = new ByteArray();
				readBytes(data);
				var length:int = data.length;
				var pos:int = int(length / 2);
				_data = new ByteArray();
				data.position = length - pos;
				data.readBytes(_data, 0, pos);
				data.position = 0;
				data.readBytes(_data, pos, length - pos);
			}
			else
			{
				_data = new ByteArray();
				readBytes(_data);
			}

			removeListener();
			_finishLoadDate = new Date();
			if(_complete != null) {
				_complete(this);
			}
		}

		public function get loadContent():ByteArray {
			return _data;
		}

		protected function onSecurityError(e:SecurityErrorEvent):void {
			_finishLoadDate = new Date();
			var url:String = this._request.url;
			if(_error != null) {
				_error(e, this);
			}
			dispose();
		}

		protected function onIoError(e:IOErrorEvent):void {
			_finishLoadDate = new Date();
			var url:String = this._request.url;
			if(_error != null) {
				_error(e, this);
			}
			dispose();
		}

		private function removeListener():void {
			this.removeEventListener(Event.COMPLETE, onComplete);
			this.removeEventListener(SecurityErrorEvent.SECURITY_ERROR, onSecurityError);
			this.removeEventListener(IOErrorEvent.IO_ERROR, onIoError);
			this.removeEventListener(ProgressEvent.PROGRESS, onProgress);
		}

		public function dispose():void {
			try {
				this.close();
			} catch(e:Error) {

			}
			removeListener();
			if(_data){
				_data.clear();
				_data = null;
			}
			_complete = null;
			_progress = null;
			_request = null;
		}

		private var _totalBytes:Number;
		private var _data:ByteArray;
		private var _error:Function;
		private var _complete:Function;
		private var _progress:Function;
		private var _request:URLRequest;
		private var _startLoadDate:Date;
		private var _finishLoadDate:Date;
	}
}